package com.gopay.bis.common.util;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.sms.client.GopaySMSHttpClient;
import com.gopay.sms.common.constants.SMSAppId;
import com.gopay.sms.common.domain.RequestVO;
import com.gopay.sms.common.domain.ResponseVO;
public class SendSmsUtils {
    private static final String SWITCH_ON = "on";
    private final static Logger logger = LoggerFactory.getLogger("smsSend");
   
	public static void sendSms(String orderId,String switchOn,String bankCode,String mobile,String content) {

        try {
        	if (SWITCH_ON.equals(switchOn)) {
                // 自定义短信内容，不提供验证码校验功能
                RequestVO req = new RequestVO();
                req.setAppId(SMSAppId._90000.value);
                req.setMobile(mobile);// 电话号码(多个以逗号分隔)
                req.setContent(bankCode+"银企直联支付:"+" "+orderId + " IP:"+getLocalIp()+"连接前置机超时 . "
                        + content);
                ResponseVO resp = GopaySMSHttpClient.getInstance().createVerificationCode(req);

                if (resp != null) {
                    if ("100".equals(resp.getStatusCode())) {
                        logger.error("银行【"+bankCode+":"+ orderId + "】预警短信发送成功");
                    } else {
                        logger.error("银行【"+bankCode+":"+orderId + "】预警短信发送失败，原因：" + resp.getMessage());
                    }
                }
            } else {
                logger.error("bis 预警短信发送已关闭，预警信息: 银行" + bankCode +"订单号："+ orderId+" 与银行服务器、前置机通信发生异常无法支付");

            }
        } catch (Exception e) {
        	e.printStackTrace();
            logger.error("senSMS error: ", e);
        }

    }
	
	public static void sendSmsErr(String switchOn,String mobile,String content) {

        try {
        	if (SWITCH_ON.equals(switchOn)) {
                // 自定义短信内容，不提供验证码校验功能
                RequestVO req = new RequestVO();
                req.setAppId(SMSAppId._90000.value);
                req.setMobile(mobile);// 电话号码(多个以逗号分隔)
                req.setContent("bis银行通道代码报错："+" IP:"+getLocalIp() +" 错误原因："
                        + content);
                ResponseVO resp = GopaySMSHttpClient.getInstance().createVerificationCode(req);

                if (resp != null) {
                    if ("100".equals(resp.getStatusCode())) {
                        logger.error("bis银行通道代码报错预警短信发送成功");
                    } else {
                        logger.error("bis银行通道代码报错预警短信发送失败，原因：" + resp.getMessage());
                    }
                }
            } else {
                logger.error("bis 预警短信发送已关闭，预警信息: bis银行通道代码报错");

            }
        } catch (Exception e) {
            logger.error("senSMS error: ", e);
        }

    }

    // 获取IP地址
    public static String getLocalIp() throws Exception {
        Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
        InetAddress ip = null;
        while (allNetInterfaces.hasMoreElements()) {
            NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
            // System.out.println(netInterface.getName());
            Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
            while (addresses.hasMoreElements()) {
                ip = (InetAddress) addresses.nextElement();
                if (ip != null && ip instanceof Inet4Address) {
                    logger.error("本机的IP = " + ip.toString());
                    return ip.toString();
                }
            }
        }
        return null;
    }
}
